<template>
	<image src="/static/image/09.png" mode="widthFix" class="bg"></image>
	<u-navbar
	title="车辆资料"
	:autoBack="true"
	:background="{
	background:'#E40417'
	}"
	:border-bottom="false"
	titleSize="28rpx" titleColor="#fff"
	back-icon-color="#fff"
	:titleBold="false"
	/>
	
	<view class="container pl30 pr30 mt20">
		<view class="box bgf  pl10 pr10 r20 ">
			<view class="t-flex center p30 bgf r20 mb20" v-if="data">
				<view class="c6 f30 bold" v-if="data.status==0">
					平台审核中...
				</view>
				<view class="c6 f30 bold t-flex center" v-if="data.status==1" style="color: #FC9F00;">
					<text class="ml20">审核通过</text>
				</view>
				<view class="" v-if="data.status==2">
					<view class="mainC f30 bold">
						审核未通过，原因：{{data.remark || '无'}}
					</view>
				</view>
			</view>
			<forms :options="form.options" v-model="form.value" ref="formsE"></forms>
		</view>
		
		
		
		
		
	</view>
	
	<view class="bpad"></view>
	<view class="bpad"></view>
	<view class="Allbottom t-flex center safeArea" >
		<view class="btn t-flex center cf f30" @click="submit">提交</view>
	</view>
</template>

<script setup lang="ts">
	import { reactive, ref } from "vue"
	import { notEmpty } from "../../../lib/ts/forms";
	import { addCar, carDetail, editCar, getCarType, getPowerType } from "../../../api";
	import { getTime, routeFn } from "../../../lib/ts/util";
	import { onLoad } from "@dcloudio/uni-app"

	const formsE = ref()
	
	onLoad(options=>{
		if(options.id) getdata(options.id)
	})
	
	let form = reactive({
		options:[
			{name:'车辆类型',type:'select',key:'car_type_id',options:[]},
			{name:'动力类型',type:'select',key:'car_type_dl_id',options:[]},
			{name:'品牌型号',type:'text',key:'pp_name'},
			{name:'车牌号码',type:'text',key:'car_number'},
			{name:'上传行驶证照片',required:true,rules:[notEmpty()],type:'images',key:'xsz_image',max:1},
			{name:'车主声明函',type:'images',key:'smh_image',tips:'（如果非本人车辆，必须需要上传）',max:1},
			{name:'身份证正反面',required:true,rule:[notEmpty()],type:'idcard',key:'sfz'},
			{name:'交强险',required:true,rules:[notEmpty()],type:'images',key:'jqx_image',max:1},
			{name:'商业险',required:true,rules:[notEmpty()],type:'images',key:'syx_image',max:1},
			{name:'到期时间',required:true,rules:[notEmpty()],type:'time',key:'bxdq_time',max:1},
			{name:'副驾驶照片',required:true,rules:[notEmpty()],type:'images',key:'fjs_image',max:1},
		],
		value:{}
	})
	
	let data = ref()
	const getdata = async (id:number)=>{
		let res = await carDetail(id)
		data.value = res
		form.options.forEach(item=>{
			if(res[item.key]){
				switch (item.type){
					case 'time':form.value[item.key] = getTime(res[item.key],2) 
					break;
					case 'images':
						form.value[item.key] = res[item.key].split(',')
						break;
					default:
						form.value[item.key] = res[item.key]
						break;
				}
			}
		})
		form.value.sfz = [res.sfz_b_image,res.sfz_a_image]
	}
	
	//选项数据
	const init = async ()=>{
		form.options[0].options = await getCarType()
		form.options[1].options = await getPowerType()
	}
	init()
	
	
	
	
	const submit = ()=>{
		formsE.value.submit( async (formdata)=>{
			if(formdata.code){
				let row = formdata.row
				row.sfz_a_image = row.sfz.split(',')[1]
				row.sfz_b_image = row.sfz.split(',')[0]
				
				let res
				if(data.value){ //编辑
					res = await editCar({id:data.value.id,...row})
				}else{ //新增
					res = await addCar(row)
				}				
				uni.$emit('checkWorkerInfo')
				uni.$emit('updateMyCar')
				routeFn.back(res.msg)
			}
		},true)
	}
	
	
	
	

</script>

<style scoped lang="scss">
	.bg{
		width: 100%;
		position: fixed;
	}
	
	.container{
		position: relative;
		z-index: 2;
	}
	
	.title{
		border-bottom: 1px solid #f6f6f6;
	}
	
	
	
	
</style>
